package servlet.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;

import servlet.ServletTemplate;
import util.Helper;

public class DeleteUser extends ServletTemplate{
    /**
     * 执行POST操作 
     */
    public void doPost(HttpServletRequest request,
                         HttpServletResponse response) throws IOException, ServletException{
        super.doPost(request, response);
        
        if(false == this.isLogin() || false == this.isAdmin()) {
            this.setRequestAttr("errorInfo", "非法访问！");
            this.forward("hint.jsp");
            return ;
        }
        
        int userId  = Helper.obj2Int(this.getRequestParam("id"));
        if(userId < 1) {
            this.setRequestAttr("errorInfo", "非法操作！");
            this.forward("hint.jsp");
            return ;
        }
        if(true == this.isSelf(userId)) {
            this.setRequestAttr("errorInfo", "不能删除自己！");
            this.forward("hint.jsp");
            return;
        }
        
        UserDao userDao     = new UserDao();
        if(false == userDao.delete(userId)) {
            this.setRequestAttr("errorInfo", "删除失败！");
        }
        this.setRequestAttr("hintInfo", "删除成功!");
        this.forward("hint.jsp");
    }
}
